package jp.co.sharp.android.parents.kidsguard.activity.edit;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.bluetooth.parentble.ParentBleTransfer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import jp.co.sharp.android.parents.kidsguard.App;
import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.activity.edit.adapter.NoteItemAdaptor;
import jp.co.sharp.android.parents.kidsguard.activity.home.HomeActivity;
import jp.co.sharp.android.parents.kidsguard.activity.setting.SettingActivity;
import jp.co.sharp.android.parents.kidsguard.common.DealWithWaitingDlg;
import jp.co.sharp.android.parents.kidsguard.common.DelDeviceConfirmDlg;
import jp.co.sharp.android.parents.kidsguard.common.ProcessWarningDlg;
import jp.co.sharp.android.parents.kidsguard.contants.Constants;
import jp.co.sharp.android.parents.kidsguard.contants.PageSerialNumConstants;
import jp.co.sharp.android.parents.kidsguard.contants.RequestMethodConstants;
import jp.co.sharp.android.parents.kidsguard.contants.RequestReceiverConstants;
import jp.co.sharp.android.parents.kidsguard.dto.BluetoothReceiverDto;
import jp.co.sharp.android.parents.kidsguard.dto.NoteDto;
import jp.co.sharp.android.parents.kidsguard.entity.KidInfoEntity;
import jp.co.sharp.android.parents.kidsguard.thread.BusinessThread;

public class NoteActivity extends AppCompatActivity {

    private DealWithWaitingDlg mDealWithWaitingDlg;
    private App mApp;
    private ParentBleTransfer mParentBleTransfer;
    private int mViewProcess = Constants.SEND_KID_IS_USING;
    /**  0: 初期化  true onLeWriteDataChanged  false:onLeListenerDataReceiveChanged */
    private boolean mEditFlag = false;
    private KidInfoEntity mKidInfoEntity;
    private NoteItemAdaptor mAdaptor;
    private ListView mListView;
    private List<NoteDto> mNoteList = new ArrayList<>();
    private List<NoteDto> mRawNoteList = new ArrayList<>();
    private boolean mUnReceiver = true;
    private JSONArray deleteList = new JSONArray();
    private JSONArray wordingList = new JSONArray();
    private List<NoteDto> mEditList = new ArrayList<>();
    private Button mSubmitBtn;
    private ScrollView mSv;
    private JSONArray noteArray;
    private final int REQUEST_CODE_ADD  = 1;
    private final int ADD  = 0;
    private static final String TAG = "TAG";
    /** ページジャンプ */
    private boolean mPageJumpFlag = false;
    /** システムホームプレス */
    private boolean mSystemHomeFlag = false;
    private boolean mScreenOperate = false;
    /** プログラムロック */
    private final String mSyncLock = "syncLock";
    /** 編集を再開しています... */
    private boolean mEditReopening = false;
    private static final int PAGE_EDIT_REOPEN = 1;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "NoteActivity_onCreate");
        setContentView(R.layout.activity_note);

        mApp=(App)getApplication();
        initReceiver();
        initActivity();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "NoteActivity_onRestart");
        synchronized (mSyncLock) {
            if (mSystemHomeFlag) {
                if (mApp.getAppStatus() != Constants.APP_STATUS_USER_PRESENT) {
                    Message msg = new Message();
                    msg.what = PAGE_EDIT_REOPEN;
                    handler.sendMessage(msg);
                }
                mSystemHomeFlag = false;
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "PowerSettingActivity_onStart");
        mApp.setAppStatus(Constants.APP_STATUS_INIT);
    }

    @Override
    protected void onStop() {
        Log.i(TAG, "NoteActivity_onStop");
        if (!mUnReceiver) {
            synchronized (mSyncLock) {
                try {
                    unregisterReceiver(receiver);
                } catch (IllegalArgumentException e) {
                    if (e.getMessage().contains("Receiver not registered")) {
                        // Ignore this exception. This is exactly what is desired
                        Log.e(TAG, e.getMessage());
                    } else {
                        // unexpected, re-throw
                        Log.e(TAG, e.getMessage());
                    }
                }
                if (mPageJumpFlag) {
                    try {
                        unregisterReceiver(systemReceiver);
                    } catch (IllegalArgumentException e) {
                        if (e.getMessage().contains("Receiver not registered")) {
                            // Ignore this exception. This is exactly what is desired
                            Log.e(TAG, e.getMessage());
                        } else {
                            // unexpected, re-throw
                            Log.e(TAG, e.getMessage());
                        }
                    }
                    mPageJumpFlag = false;
                }
            }
            mUnReceiver = true;
        }
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.i(TAG, "NoteActivity_onDestroy");
        if (!mUnReceiver) {
            synchronized (mSyncLock) {
                try {
                    unregisterReceiver(receiver);
                } catch (IllegalArgumentException e) {
                    if (e.getMessage().contains("Receiver not registered")) {
                        // Ignore this exception. This is exactly what is desired
                        Log.e(TAG, e.getMessage());
                    } else {
                        // unexpected, re-throw
                        Log.e(TAG, e.getMessage());
                    }
                }
                if (mPageJumpFlag) {
                    try {
                        unregisterReceiver(systemReceiver);
                    } catch (IllegalArgumentException e) {
                        if (e.getMessage().contains("Receiver not registered")) {
                            // Ignore this exception. This is exactly what is desired
                            Log.e(TAG, e.getMessage());
                        } else {
                            // unexpected, re-throw
                            Log.e(TAG, e.getMessage());
                        }
                    }
                    mPageJumpFlag = false;
                }
            }
            mUnReceiver = true;
        }
        super.onDestroy();
    }

    private void initActivity() {
        mSv = findViewById(R.id.main_scroll);
        mSv.smoothScrollTo(0, 0);

        Log.i(TAG, "NoteActivity_initActivity");
        ImageView imageview= findViewById(R.id.addNote);
        imageview.setOnClickListener(v -> {
            Intent intent=new Intent(NoteActivity.this, NoteEditActivity.class);
            intent.putExtra("type", ADD);
            startActivityForResult(intent, REQUEST_CODE_ADD);

        });

        mListView = findViewById(R.id.noteListView);

        mKidInfoEntity = ((App)getApplication()).findKidInfoBySelect();
        TextView mKidNameTv = findViewById(R.id.textViewKidName);
        String kidNameDescribe = mKidInfoEntity.getKidName()+"のキッズケータイ";
        mKidNameTv.setText(kidNameDescribe);

        mSubmitBtn = findViewById(R.id.submitBtn);
        mSubmitBtn.setEnabled(false);
        mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_normal, null));

        mSubmitBtn.setOnClickListener(v -> {
            if (mSubmitBtn.isEnabled()) {
                // Dlg
                String message = "キッズケータイへ反映中..";
                mDealWithWaitingDlg = new DealWithWaitingDlg(NoteActivity.this, R.style.commonDlg, message, true);
                mDealWithWaitingDlg.show();
                sendToKid();
                mSubmitBtn.setEnabled(false);
                mEditFlag = false;
            }
        });

        String message = "キッズケータイから情報を取得しています...";
        mDealWithWaitingDlg = new DealWithWaitingDlg(NoteActivity.this, R.style.commonDlg, message, true);
        mDealWithWaitingDlg.show();
        // init
        sendIsUsingToKid();
    }


    /**
     * 初期化Receiver
     */
    private void initReceiver() {
        Log.i(TAG, "NoteActivity_initReceiver");
        mKidInfoEntity = mApp.findKidInfoBySelect();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        // Bluetooth
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        //kid戻りデータ
        filter.addAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED);
        //isFinish
        filter.addAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_WRITE_DATA_CHANGED);
        registerReceiver(receiver, filter);
        registerReceiver(systemReceiver, filter);
        this.mParentBleTransfer = mApp.getmParentBleTransfer();
        mUnReceiver = false;
    }

    public JSONObject getInitData(){
        Log.i(TAG, "NoteActivity_getInitData");
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("method", RequestMethodConstants.METHOD_GET_WORDING_LIST);
            jsonObject.put("data",JSONObject.NULL);
        }catch (Exception e){
            e.printStackTrace();
        }
        return jsonObject;
    }

    public void initNoteList() throws Exception{
        Log.i(TAG, "NoteActivity_initNoteList");
        mNoteList.clear();
        int length = noteArray.length();
        for (int i = 0; i < length; i++) {
            JSONObject data = (JSONObject)noteArray.get(i);
            NoteDto noteDto = new NoteDto();
            noteDto.setId(data.getInt("id"));
            noteDto.setText(data.getString("text"));
            mNoteList.add(noteDto);
            mRawNoteList.add(noteDto);
        }
    }

    public void setAdaptor(){
        Log.i(TAG, "NoteActivity_setAdaptor");
        mAdaptor = new NoteItemAdaptor(NoteActivity.this, R.layout.activity_note_item, mNoteList);
        mListView.setAdapter(mAdaptor);
    }

    /**
     * システム放送
     */
    private final BroadcastReceiver systemReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "NoteActivity_systemReceiver");
            try {
                String action = intent.getAction();
                if (Intent.ACTION_SCREEN_OFF.equals(action)) {
                    synchronized (mSyncLock) {
                        mApp.setAppStatus(Constants.APP_STATUS_SCREEN_OFF);
                        mScreenOperate = true;
                        sendExitToKid();
                    }
                } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
                    synchronized (mSyncLock) {
                        mApp.setAppStatus(Constants.APP_STATUS_USER_PRESENT);
                        if (!mPageJumpFlag) {
                            Message msg = new Message();
                            msg.what = PAGE_EDIT_REOPEN;
                            handler.sendMessage(msg);
                        }
                    }
                } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
                    synchronized (mSyncLock) {
                        mApp.setAppStatus(Constants.APP_STATUS_CLOSE_SYSTEM_DIALOGS);
                        mSystemHomeFlag = true;
                        mScreenOperate = true;
                        sendExitToKid();
                    }
                } else if (RequestReceiverConstants.RECEIVER_BLUETOOTH_CONNECT_STATE_CHANGED.equals(action)) {
                    actionStateChanged(intent);
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    };

    /**
     * BroadcastReceiver
     */
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "NoteActivity_receiver");
            try {
                String action = intent.getAction();
                if (RequestReceiverConstants.RECEIVER_BLUETOOTH_WRITE_DATA_CHANGED.equals(action)) {
                    receiverBluetoothWriteDataChanged(intent);
                } else if (RequestReceiverConstants.RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED.equals(action)) {
                    receiverBluetoothListenerDataChanged(intent);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    /***
     * ブルートゥースの状態変化
     * @param  intent
     */
    private void actionStateChanged(Intent intent) {
        // 初期化
        BluetoothReceiverDto bluetoothReceiverDto = (BluetoothReceiverDto) intent.getSerializableExtra(RequestReceiverConstants.RECEIVER_KEY);
        String macAddress = bluetoothReceiverDto.getAddress();
        if (mApp.isMimamoriCheck(macAddress)) {
            mApp.addmLsIsMimamori(macAddress);
            // TODO Mimamori
            mViewProcess = Constants.SEND_KID_EXIT;
            sendExitToKid();
            // ページジャンプ
            mPageJumpFlag = true;
            Intent intent1 = new Intent();
            intent1.setAction(Constants.ACTION_IS_MIMAMORI);
            intent1.setClass(NoteActivity.this, HomeActivity.class);
            startActivity(intent1);
        } else {
            mKidInfoEntity.setBtConnStatus(Constants.DEVICE_DISCONNECTION_SUCCESS);
            mApp.updateKidInfo(mKidInfoEntity);

            String message = mKidInfoEntity.getKidName() + "と切断されました。編集を中断します。";
            ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(NoteActivity.this, R.style.commonDlg, message);
            processWarringDlg.show();
            processWarringDlg.setOnOperateSetListener(() -> {
                processWarringDlg.dismiss();
                // ページジャンプ
                mPageJumpFlag = true;
                Intent intent1 = new Intent();
                intent1.setClass(NoteActivity.this, SettingActivity.class);
                startActivity(intent1);
            });
        }
    }

    /**
     * 受信機Bluetooth書き込みデータを変更
     * @param intent
     */
    private void receiverBluetoothWriteDataChanged(Intent intent) {
        BluetoothReceiverDto bluetoothReceiverDto = (BluetoothReceiverDto) intent.getSerializableExtra(RequestReceiverConstants.RECEIVER_KEY);
        if (bluetoothReceiverDto.isFinished()) {
            switch (mViewProcess) {
                case Constants.SEND_KID_ENTER:
                    mViewProcess = Constants.SEND_KID_GET_INFO;
                    if (!mScreenOperate) {
                        new BusinessThread(Constants.SEND_KID_GET_INFO, mParentBleTransfer, mKidInfoEntity, getInitData(), PageSerialNumConstants.PAGE_SERIAL_NOTE).start();
                    } else {
                        mEditReopening = false;
                        mScreenOperate = false;
                        handler.sendEmptyMessage(0);
                    }
                    break;
                case Constants.SEND_KID_EXIT:
                    if (mDealWithWaitingDlg != null && mDealWithWaitingDlg.isShowing()) {
                        mDealWithWaitingDlg.dismiss();
                    }
                    if (!mScreenOperate) {
                        mPageJumpFlag = true;
                        NoteActivity.this.finish();
                    } else if (!mSystemHomeFlag) {
                        // ページジャンプ
                        mPageJumpFlag = true;
                        Intent intent1 = new Intent();
                        intent1.setClass(NoteActivity.this, EditActivity.class);
                        startActivity(intent1);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * リスナーデータ変更
     * @param intent
     */
    private void receiverBluetoothListenerDataChanged(Intent intent) throws Exception{
        BluetoothReceiverDto bluetoothReceiverDto = (BluetoothReceiverDto) intent.getSerializableExtra(RequestReceiverConstants.RECEIVER_KEY);
        String retStr = bluetoothReceiverDto.getRetStr();
        JSONObject jsonObject = new JSONObject(retStr);

        if (RequestMethodConstants.METHOD_GET_WORDING_LIST.equals(jsonObject.getString("method"))) {
            mDealWithWaitingDlg.dismiss();
            noteArray = jsonObject.getJSONArray("data");
            if (!mEditFlag) {
                initNoteList();
                setAdaptor();
            }
        } else if (RequestMethodConstants.METHOD_GET_OPERATING_STATE.equals(jsonObject.getString("method"))) {
            JSONObject data = jsonObject.getJSONObject("data");
            if (Constants.IS_USING_OK.equals(data.getString("state"))) {
                //send enter
                sendEnterToKid();
            } else {
                mDealWithWaitingDlg.dismiss();
                String message = "別のスマートフォンから操作中のため、設定を変更できません。";
                ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(NoteActivity.this, R.style.commonDlg, message);
                processWarringDlg.show();
                processWarringDlg.setOnOperateSetListener(() -> {
                    processWarringDlg.dismiss();
                    NoteActivity.this.finish();

                });
            }
        } else if (RequestMethodConstants.METHOD_ON_SYNC_RESULT.equals(jsonObject.getString("method"))) {
            if (Constants.ON_SYNC_RESULT.equals(jsonObject.getString("result"))) {
                handler.sendEmptyMessage(0);
                mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_normal, null));
                sendExitToKid();
            } else {
                handler.sendEmptyMessage(0);
                String message = "キッズケータイへ反映できませんでした";
                ProcessWarningDlg processWarningDlg = new ProcessWarningDlg(NoteActivity.this, R.style.commonDlg, message);
                processWarningDlg.show();
                processWarningDlg.setOnOperateSetListener(processWarningDlg::dismiss);
            }
        } else if (RequestMethodConstants.METHOD_ON_OPERATE_INTERRUPT.equals(jsonObject.getString("method"))) {
            String message = "キッズケータイが操作されました。編集を中断します。";
            ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(NoteActivity.this, R.style.commonDlg, message);
            processWarringDlg.show();
            processWarringDlg.setOnOperateSetListener(() -> {
                processWarringDlg.dismiss();
                NoteActivity.this.finish();
            });
        }
    }




    /**
     * is usingを送信
     */
    private void sendIsUsingToKid() {
        try {
            mViewProcess = Constants.SEND_KID_IS_USING;
            new BusinessThread(Constants.SEND_KID_IS_USING,
                    mParentBleTransfer,
                    mKidInfoEntity,
                    null,
                    PageSerialNumConstants.PAGE_SERIAL_NOTE).start();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    /**
     * enterを送信
     */
    private void sendEnterToKid() {
        try {
            mViewProcess = Constants.SEND_KID_ENTER;
            new BusinessThread(Constants.SEND_KID_ENTER,
                    mParentBleTransfer,
                    mKidInfoEntity,
                    null,
                    PageSerialNumConstants.PAGE_SERIAL_NOTE).start();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }


    /**
     * exitを送信
     */
    private void sendExitToKid() {
        try {
            mViewProcess = Constants.SEND_KID_EXIT;
            new BusinessThread(Constants.SEND_KID_EXIT,
                    mParentBleTransfer,
                    mKidInfoEntity,
                    null,
                    PageSerialNumConstants.PAGE_SERIAL_NOTE).start();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }

    }


    /**
     * 子供にデータを送信
     */
    private void sendToKid() {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("method", RequestMethodConstants.METHOD_SET_WORDING_LIST);
            JSONObject jsonData = new JSONObject();

            jsonData.put("wordingList", wordingList);
            if(deleteList != null && deleteList.length() > 0) {
                jsonData.put("deleteList", deleteList);
            }
            jsonObject.put("data", jsonData);
            mViewProcess = Constants.SEND_KID_SET_INFO;
            new BusinessThread(Constants.SEND_KID_SET_INFO,
                    mParentBleTransfer,
                    mKidInfoEntity,
                    jsonObject,
                    PageSerialNumConstants.PAGE_SERIAL_NOTE).start();
        } catch (JSONException e) {
            Log.e(TAG, e.getMessage());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        NoteDto noteDto = (NoteDto)data.getSerializableExtra("data");
        if (noteDto.getId() != 0) {
            int position = data.getIntExtra("position", 0);
            if (data.getBooleanExtra("isDel", false)) {
                if (noteDto.getId() != -1) {
                    deleteList.put(noteDto.getId());
                }
                mNoteList.remove(position);
            } else {
                JSONObject note = new JSONObject();
                try {
                    note.put("id", noteDto.getId());
                    note.put("text", noteDto.getText());
                } catch (JSONException e) {
                    Log.e(TAG, e.getMessage());
                }
                wordingList.put(note);
                if (noteDto.getId() == -1) {
                    mEditList.add(noteDto);
                    mNoteList.add(noteDto);
                } else {
                    mNoteList.set(position, noteDto);
                    boolean isExist = false;
                    for(int i = 0; i < mEditList.size(); i++) {
                        if (mEditList.get(i).getId() == noteDto.getId()) {
                            mEditList.set(i, noteDto);
                            isExist = true;
                        }
                    }
                    if(!isExist) {
                        mEditList.add(noteDto);
                    }
                }
            }
            if (deleteList.length() == 0 && mEditList.size() > 0) {
                for(NoteDto dto : mEditList) {
                    if(dto.getId() == -1) {
                        mSubmitBtn.setEnabled(true);
                        mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_change, null));
                        break;
                    } else {
                        for(NoteDto rawDto : mRawNoteList) {
                            if(rawDto.getId() == dto.getId()) {
                                if(rawDto.getText().equals(dto.getText())) {
                                    mSubmitBtn.setEnabled(false);
                                    mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_normal, null));
                                } else {
                                    mSubmitBtn.setEnabled(true);
                                    mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_change, null));
                                    break;
                                }
                            }
                        }
                    }
                }
            } else if (deleteList.length() == 0 && mEditList.size() == 0) {
                mSubmitBtn.setEnabled(false);
                mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_normal, null));
            } else {
                mSubmitBtn.setEnabled(true);
                mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_change, null));
            }
            setAdaptor();
        }
        mEditFlag = true;
        initReceiver();
    }


    /**
     * Dialog閉じる
     */
    private Handler handler = new Handler(msg -> {
        if (null != mDealWithWaitingDlg && mDealWithWaitingDlg.isShowing()) {
            mDealWithWaitingDlg.dismiss();
        } else if (msg.what == PAGE_EDIT_REOPEN) {
            if (!mEditReopening) {
                String message = "編集を再開しています...";
                mDealWithWaitingDlg = new DealWithWaitingDlg(NoteActivity.this, R.style.commonDlg, message, false);
                mDealWithWaitingDlg.show();
                mScreenOperate = true;
                mViewProcess = Constants.SEND_KID_IS_USING;
                new BusinessThread(Constants.SEND_KID_IS_USING,
                        mParentBleTransfer,
                        mKidInfoEntity,
                        null,
                        PageSerialNumConstants.PAGE_SERIAL_NUM_POWER_SETTING).start();
                // ブロードキャストの再登録
                unregisterReceiver(systemReceiver);
                initReceiver();
                mEditReopening = true;
            }
        }
        return false;
    });

    /**
     * ページリターンキー、フルスクリーンリターンジェスチャー
     */
    @Override
    public void onBackPressed() {
        backActivity(null);
    }

    /**
     * 前のページに戻る(値なし)
     */
    public void backActivity(View view) {

        if (!mSubmitBtn.isEnabled() ) {
            sendExitToKid();
        } else {
            String message = "編集内容は保存されていません。編集内容を破棄して前の画面に戻りますか？";
            DelDeviceConfirmDlg mDelDeviceConfirmDlg = new DelDeviceConfirmDlg(NoteActivity.this, R.style.commonDlg, message);
            mDelDeviceConfirmDlg.show();
            mDelDeviceConfirmDlg.setOnOperateSetListener(this::sendExitToKid);
        }
    }


}
